/**
 * 将一个数组选择K步
 */
//时间复杂度O(n^2)
const roateArr = (array:string[],key:number):string[]=>{
    if(array.length === 0) return array
    const step = Math.abs(key % array.length)
    for(let i = 0;i<step;i++){
        let pop = array.pop()
        //unshift的操作很慢 此处相当于一个O(N)的复杂度
        array.unshift(pop!)
    }
    return array
}


//时间复杂度O(1)
const roateArr2 = (array:string[],key:number)=>{
    const length = array.length
    if(array.length === 0) return array
    const step = Math.abs(key % array.length)
    let slicearr = array.slice(-step)
    let rightarr = array.slice(0,length - key)
    return slicearr.concat(rightarr)
}

// console.log(roateArr2(['1','2','3','4','5','6','7','8'],3))

/**
 * 性能测试
 */

let arr1 = []
for(let i = 0;i<10 *10000;i++){
    arr1.push(i+'')
}

console.time('roate1')
roateArr(arr1,9*10000)
console.timeEnd('roate1')


let arr2 = []
for(let i = 0;i<10 *10000;i++){
    arr1.push(i+'')
}

console.time('roate2')
roateArr2(arr1,9*10000)
console.timeEnd('roate2')